class ViewMenu

  attr_accessor :cur_link
  
  def initialize menu = []
    @menu = menu
    @cur_link = 'main'
  end

  def make_menu menu, cur_link
    res = ''
        menu.each do |menu_item|
          res += view_menu_item menu_item, cur_link unless menu_item.nil?          
        end
        res
  end
    
  def html_format       
    res = '<ul>'  
    res += make_menu @menu, @cur_link
    res += '</ul>'
  end
    
  def active_menu? menu_item, cur_link
    return true if menu_item.active == cur_link
    menu_item.submenu.each do |submenu_item |
     return true if active_menu? submenu_item, cur_link        
    end
    false
  end
    
  def view_menu_item menu_item, cur_link = ''
    res = '<li>'
    active = " class='current'" if active_menu? menu_item, cur_link
    res += "<a href='#{menu_item.link}'#{active}>#{menu_item.name}</a>"
    res += '</li>'
  end
    
    
  def html_format_sub
    res = '<ul>'  
      @menu.each do |menu_item|
        res += make_menu menu_item.submenu, @cur_link if active_menu? menu_item, @cur_link
      end
    res += '</ul>'
  end

end
  
